package com.ts.webapplication.controller;

import com.ts.webapplication.entity.User;
import com.ts.webapplication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * @author tiansong
 * @create 2019/6/1/001 - {TIME}
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/userLogin")
    public String userLogin(Model model, @RequestParam("username") String name, @RequestParam("password") String password){

        if (name.equals("admin")&&password.equals("admin")){
            ArrayList<User> users = userService.findAll();
            model.addAttribute("users",users);
            return "redirect:/list.html";

        }else {

            model.addAttribute("msg","用户名或者密码错误！");
           return "login";
        }

    }
    @RequestMapping("/findAll")
    public String findAll(Model model){
        ArrayList<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "list";
    }

    @RequestMapping("/delete")
    public ModelAndView delete(Model model,User user){
        ModelAndView modelAndView = new ModelAndView();
        userService.delete(user);
        ArrayList<User> users = userService.findAll();
        model.addAttribute("users",users);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("/update")
    public ModelAndView update(Model model,User user1){
        ModelAndView modelAndView = new ModelAndView();
        /*User user1 = userService.find(user);*/
        /*ArrayList<User> users = userService.findAll();*/
        model.addAttribute("user1",user1);
        modelAndView.setViewName("update");
        return modelAndView;
    }

    @RequestMapping("/handleUpdate")
    public ModelAndView handleUpdate(Model model,User user1){
        ModelAndView modelAndView = new ModelAndView();
        userService.update(user1);
        ArrayList<User> users = userService.findAll();
        model.addAttribute("users",users);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("/handleAddUser")
    public ModelAndView handleAddUser(Model model,User user1){
        ModelAndView modelAndView = new ModelAndView();
        userService.addUser(user1);
        ArrayList<User> users = userService.findAll();
        model.addAttribute("users",users);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("/query")
    public String Query(Model model, User user){
        ArrayList<User> users = userService.findByName(user);
        model.addAttribute("users",users);
        return "list";
    }


}
